<template>
  <div class="ErpRecord">

    <div class="publicSearch">
      <div class="searchBar" :style="{height: isMoreSearch?'auto':''}">
        <el-button-group>
          <el-button @click="getInventoryBalanceData" type="primary" size="mini" icon="el-icon-search">查询</el-button>
          <el-button @click="clearSearchData" size="mini" icon="el-icon-refresh">重置</el-button>
        </el-button-group>
        <div>
          <span>类型</span>
          <el-select v-model="searchData.type" size="mini" filterable placeholder="请选择接口" clearable
                     @change="handleChange"
          >
            <el-option
                v-for="r in stationList"
                :key="r.label"
                :label="r.label"
                :value="r.label">
            </el-option>

          </el-select>
        </div>
        <div>
          <span>状态</span>
          <el-select v-model="searchData.state" size="mini" filterable placeholder="请选择状态" clearable>
            <el-option label="开启" value=0></el-option>
            <el-option label="关闭" value=1></el-option>
          </el-select>
        </div>
        <div>
          <span>关键字</span>
          <el-input v-model="searchData.keyWords" size="mini" placeholder="单号/参数" clearable></el-input>
        </div>
<!--        <div>-->
<!--          <span>同步日期</span>-->
<!--          <el-date-picker-->
<!--              v-model="searchData.startTime"-->
<!--              size="mini"-->
<!--              type="date"-->
<!--              value-format="yyyy-MM-dd"-->
<!--              placeholder="请选择日期">-->
<!--          </el-date-picker>-->
<!--        </div>-->
      </div>

      <span class="moreSearchBtn"
            :class="isMoreSearch?'el-icon-caret-top':'el-icon-caret-bottom'"
            @click="isMoreSearch = !isMoreSearch"></span>
    </div>

    <div class="headerBtns">
      <!--<el-button
              size="small"
              type="primary"
              round
              icon="iconfont icon-shangjia"
              @click="exportExcel()"
      >导出</el-button>-->
    </div>
    <div class="tableBox rowHeight">
      <el-table
          v-loading="loading"
          border
          highlight-current-row
          :data="inventoryBalances"
          height="calc(100% - 50px)"
          @selection-change="handleSelectionChange"
          style="width: 100%">
        <!-- <el-table-column
          fixed="left"
          type="selection"
          width="55">
        </el-table-column>
        -->

        <!--<el-table-column
           show-overflow-tooltip
           prop="type"
           label="类型"
           min-width="100">
           <template slot-scope="scope">
             <span  v-if="scope.row.type==1" style="color:#409EFF">入库</span>
             <span  v-if="scope.row.type==2"  style="color:#E6A23C"  >出库</span>
           </template>
         </el-table-column>-->
        <el-table-column
            show-overflow-tooltip
            prop="requestUrl"
            label="url"
            min-width="130">

        </el-table-column>
        <el-table-column
            show-overflow-tooltip
            prop="resultCode"
            label="状态"
            min-width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.resultCode!==200" style="color:#F56C6C">同步失败</span>
            <span v-if="scope.row.resultCode===200" style="color:#67C23A">同步成功</span>
          </template>
        </el-table-column>
        <el-table-column
            show-overflow-tooltip
            prop="beginTime"
            label="请求时间"
            min-width="120">
        </el-table-column>
        <el-table-column
            show-overflow-tooltip
            prop="endTime"
            label="响应时间"
            min-width="120">
        </el-table-column>
        <el-table-column
            show-overflow-tooltip
            prop="requestParams"
            label="参数"
            min-width="230">
        </el-table-column>
        <el-table-column
            show-overflow-tooltip
            prop="retryNumber"
            label="重试次数"
            min-width="230">
        </el-table-column>

        <el-table-column
            show-overflow-tooltip
            prop="enable"
            label="enable"
            min-width="50">
          <template slot-scope="scope">
            <span v-if="scope.row.enable===1" style="color:#67C23A">关闭</span>
            <span v-if="scope.row.enable!=1" style="color:#F56C6C">开启</span>
          </template>
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">重试</el-button>
            <el-button @click="onClose(scope.row)" type="text" size="small">关闭</el-button>
          </template>
        </el-table-column>

      </el-table>
      <el-pagination
          class="pagination-bottom"
          style="text-align: right;"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-sizes="[15,30,50,100]"
          :current-page.sync="searchData.pageNum"
          :page-size="searchData.pageSize"
          layout="sizes, total, prev, pager, next, jumper"
          :total="searchData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import DateUtils from "../../../utils/DateUtils";

export default {
  name: 'ErpRecord',
  data() {
    return {
      loading: false,
      isMoreSearch: false,
      stationList: [
        {
          label: '单台套生产需求任务'
        }, {
          label: '完工入库'
        }, {

          label: '计划外领料申请'
        }

      ],

      searchData: {
        pageNum: 1,
        pageSize: 15,
        total: null,
        startTime: '',
        endTime: '',
        keyWords: '',
        //wareId: PF.getLocal('wareId', 'number'),
        organizationId: null,
        type: '',
        state: ''

      },

      inventoryBalances: [],
      organizations: [],
      multipleSelection: [],
      freezeReasons: [],

      dialogNewVisible: false,
      dialogUpdateVisible: false,
      dialogFreezeVisible: false,

      formLabelWidth: '120px',

      form: {
        freezeMaster: {
          freezeDescribe: '',
          memo: '',
          wareId: null,
          wareName: '',
          organizationId: null,
          organizationName: '',
        },
        freezeDetails: []
      },
      rules: {},

    }
  },
  computed: {},
  methods: {
    //导出
    exportExcel() {
      let url = localStorage.getItem("baseUrl");
      //let token = this.$tool.localGet("token");
      window.location.href = url + "/erpBack/exportListInfo?type=" + this.searchData.type + "&state=" + this.searchData.state +
          "&startTime=" + this.searchData.startTime;
    },

    handleClick(val) {
      IOT.getServerData('/timed_out_log/retry', 'post', val, (ret) => {
        this.loading = false;
        if (ret.code === 200) {
          IOT.tips("操作成功！", "success", 1000);
          this.getInventoryBalanceData();
        } else {
          IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
        }
      });
    },
    onClose(val) {
      IOT.getServerData('/timed_out_log/onClose', 'post', val, (ret) => {
        this.loading = false;
        if (ret.code === 200) {
          IOT.tips("操作成功！", "success", 1000);
          this.getInventoryBalanceData();

        } else {
          IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
        }
      });
    },
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getInventoryBalanceData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getInventoryBalanceData()
    },
    clearSearchData() {
      this.searchData.type = null;
      this.searchData.state = null;
      this.searchData.startTime = '';
      this.searchData.endTime = '';
    },
    getInventoryBalanceData() {
      let searchData = PF.JSON(this.searchData);
      if (searchData.startTime) {
        let endTimestamp = new Date(searchData.startTime).getTime() + 3600 * 1000 * 24
        searchData.endTime = DateUtils.timeToStr(endTimestamp, "yyyy-MM-dd")
      }
      this.loading = true;
      IOT.getServerData('/timed_out_log/page', 'get', searchData, (ret) => {
        console.log("查询erp回传记录返回：", ret)
        this.loading = false;
        if (ret.code === 200) {
          let list = ret.rows;
          this.inventoryBalances = list;
          this.searchData.total = ret.total;
          this.searchData.pageNum = ret.pageNumber
        } else {
          IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
        }
      });
    },
    handleChange(val) {
      this.getInventoryBalanceData()
      // console.log(this.searchData.type)
      // console.log(val)
    },
    exportInventory() {
      let searchData = PF.JSON(this.searchData);
      if (!PF.isNull(searchData.itemCode)) {
        delete searchData.itemCode
      }
      if (!PF.isNull(searchData.itemName)) {
        delete searchData.itemName
      }

      searchData.pageSize = searchData.pageSize;
      IOT.showOverlay('正在导出，请稍等...');


      IOT.getServerData('/erp/records/list', 'get', searchData, (ret) => {
        IOT.hideOverlay();
        if (ret.code === 200) {
          var path = ret.data;

          window.open(path)
        } else {
          IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
        }
      }, true);
    },
    resetForm(formName) { // 重置
      this.$refs[formName].resetFields();
    },


    handleSelectionChange(val) {
      this.multipleSelection = val;
    },


    changePackDetailId(index) {

      let detail = PF.JSON(this.inventoryBalances[index]);
      for (let i = 0; i < detail.packDetails.length; i++) {
        if (detail.packDetails[i].packDetailId1 === detail.packDetailId) {
          this.inventoryBalances[index].packDescribe = detail.packDetails[i].packDescribe1;
          this.inventoryBalances[index].transRatio = detail.packDetails[i].transRatio1;
        }
      }
    },
  },
  created() {
    this.getInventoryBalanceData();

    PF.getOrganizations((rows) => {
      this.organizations = rows;
    });
  },
  mounted() {
  }
}
</script>

<style scoped lang="less">
.ErpRecord {

}
</style>
